package community.interfaces.controller;

import community.application.service.PostService;
import community.domain.model.Post;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/posts")
@RequiredArgsConstructor
public class PostController {

    private final PostService postService;

    @PostMapping
    public Post create(@RequestBody Post post) {

        return postService.createPost(post);
    }

    @GetMapping("/{id}")
    public Post getById(@PathVariable Long id) {
        return postService.getPostById(id);
    }

    @GetMapping
    public List<Post> getAll() {
        return postService.getAllPosts();
    }

    @PutMapping("/{id}")
    public Post update(@PathVariable Long id, @RequestBody Post updatedPost) {
        updatedPost.setPostId(id);
        return postService.updatePost(updatedPost);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        postService.deletePost(id);
    }

    // 添加这个接口
    @GetMapping("/{id}/comment-count")
    public Long getCommentCount(@PathVariable Long id) {
        return postService.getCommentCount(id);
    }
}
